//
//  AddTabBar.m
//  MyFramework
//
//  Created by 余洋 on 2018/10/22.
//  Copyright © 2018 余洋. All rights reserved.
//

#import "AddTabBar.h"

@implementation AddTabBar

- (instancetype)init {
    self = [super init];
    if (self) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [btn setImage:[UIImage imageNamed:@"plus_Last"] forState:UIControlStateNormal];
        //取消点击阴影效果
        btn.adjustsImageWhenHighlighted = NO;
        btn.bounds = CGRectMake(0, 0, 64, 64);
        self.centerBtn = btn;
        [self addSubview:btn];
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    self.centerBtn.center = CGPointMake(SCREEN_WIDTH * 0.5, self.bounds.size.height * 0.2);
    //计算tabbar每个按钮的宽度
    CGFloat wigth = SCREEN_WIDTH / 5;
    int index = 0;
    for (UIView *subView in self.subviews) {
        if ([subView isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
            subView.frame = CGRectMake(index * SCREEN_WIDTH/5, subView.bounds.origin.y, wigth, subView.bounds.size.height);
            index++;
            if (index == 2) {
                index++;
            }
        }
    }
}

//重写hitTest方法，去监听"+"按钮的点击，目的是为了让凸出的部分点击也有反应
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
    if (self.isHidden == NO) {
        CGPoint newPoint = [self convertPoint:point toView:self.centerBtn];
        if ([self.centerBtn pointInside:newPoint withEvent:event]) {
            return self.centerBtn;
        } else {
            return [super hitTest:point withEvent:event];
        }
    } else {
        return [super hitTest:point withEvent:event];
    }
}
@end
